function setCtx(ctx) {
	this.ctx = ctx;
	
}
var Class = {
    create : function() {
        return function() {
            this.initialize.apply(this, arguments);
        }
    }
}
var Extend = function(destination, source) {
    for ( var property in source) {
        destination[property] = source[property];
    }
}
function stopDefault(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    } else {
        window.event.returnValue = false;
    }
    return false;
}
/**
 * 星星打分组件
 */

var score;
$(function (){
    var Stars = Class.create();
    Stars.prototype = {
            initialize: function(star,options) {
                this.SetOptions(options); //默认属性
            var flag = 999; //定义全局指针
            var isIE = (document.all) ? true : false; //IE?
            var starlist = document.getElementById(star).getElementsByTagName('a'); //星星列表
            var input = document.getElementById(this.options.Input) || document.getElementById(star+"-input"); // 输出结果
           
            var tips = document.getElementById(this.options.Tips) || document.getElementById(star+"-tips"); // 打印提示
            var nowClass = " " + this.options.nowClass; // 定义选中星星样式名
            var tipsTxt = this.options.tipsTxt; // 定义提示文案
            var len = starlist.length; //星星数量
            var tempValue = "";
            for(i=0;i<len;i++){ // 绑定事件 点击 鼠标滑过
                starlist[i].value = i;
                starlist[i].onclick = function(e){
                    stopDefault(e);
                    this.className = this.className + nowClass;
                    flag = this.value;
                    input.value = this.getAttribute("star:value");
                    tips.innerHTML = tipsTxt[this.value];
                    tempValue = tipsTxt[this.value];
                    $("#level").val(input.value);
                }
                starlist[i].onmouseover = function(){
                    if (flag < 999){
                        var reg = RegExp(nowClass,"g");
                        tips.innerHTML = tipsTxt[this.value];
                        starlist[flag].className = starlist[flag].className.replace(reg,"");
                        //tips.innerHTML = tipsTxt[this.value]
                    }
                }
                starlist[i].onmouseout = function(){
                    if (flag < 999){
                        starlist[flag].className = starlist[flag].className + nowClass;
                    }
                    tips.innerHTML = tempValue;
                }
            };
            if (isIE){ //FIX IE下样式错误
                var li = document.getElementById(star).getElementsByTagName('li');
                for (var i = 0, len = li.length; i < len; i++) {
                    var c = li[i];
                    if (c) {
                        c.className = c.getElementsByTagName('a')[0].className;
                    }
                }
            }
        },
        //设置默认属性
        SetOptions: function(options) {
            this.options = {//默认值
                Input:      "",//设置触保存分数的INPUT
                Tips:       "",//设置提示文案容器
                nowClass:   "current-rating",//选中的样式名
                tipsTxt:    ["1星 - 轻微","2星 - 不是很严重","3星- 有点严重","4星 - 严重","5星 - 非常严重"]//提示文案
            };
            Extend(this.options, options || {});
        }
    }
	var Stars1 = new Stars("stars1",{nowClass:"current-rating",tipsTxt:["1星 - 轻微","2星 - 不是很严重","3星- 有点严重","4星 - 严重","5星 - 非常严重"]});
})

function checkTitle(){
	var title = $("#title").val();
	if(title == null || title == ""){
		layer.tips('标题不能为空!', '#title', {
			tips: 3,
			time:1000
		});
		return false;
	} else {
		return true;
	}
}

function checkHouse(){
	var houseId = $("#houseId").val();
	if(houseId == null || houseId == ""){
		layer.tips('请选择房屋!', '#houseId', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

function checkContactor(){
	var contactor = $("#contactor").val();
	if(contactor == null || contactor == ""){
		layer.tips('请填写联系人!', '#contactor', {
			tips: 3,
			time:1000
		});
		return false;
	} else{
		return true;
	}
}

function checkContactorPhone(){
	var contactorPhone = $("#contactorPhone").val();
	if(contactorPhone == $("#contactorPhone").attr("placeholder")){
		contactorPhone = "";
	}
	if(contactorPhone == null || contactorPhone == ""){
		layer.tips('请填写联系电话!', '#contactorPhone', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		var reg = /^(0[0-9]{2,3}\-?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0-9]|18[5|6|8|9])\d{8}$)/;
		if (!reg.test(contactorPhone)) {
			layer.tips('手机号格式错误，请填写正确格式的手机号!有区号的必须加上短横线，如：027-87878787；027-59595959', '#contactorPhone', {
				tips : 3
			});
			return false;
		} else {
			return true;
		}
	}
}

function checkContent(){
	var content = $("#content").val();
	if(content == null || content == ""){
		layer.tips('请填写问题描述!', '#content', {
			tips: 3,
			time:1000
		});
		return false;
	} else{
		return true;
	}
}

function checkLevel(){
	var level = $("#level").val();
	if(level == null || level == ""){
		layer.tips('请选择危害程度!', '#stars1', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

/*function checkType(){
	var type = $("#type").val();
	if(type == null || type == "" || type == "-1"){
		layer.tips('请选择类型!', '#type', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}*/

function checkPhoto(){
	var imgLength = $("#imgLength").val();
	if(imgLength == 0 || imgLength == ""){
		layer.tips('请上传照片,最多五张！!', '#imgArea', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

function checkCommon(){
	var common = $("#common").val();
	if(common == null || common == ""){
		layer.tips('请填写评论!', '#common', {
			tips: 3,
			time:1000
		});
		return false;
	} else{
		return true;
	}
}

function showdelete(target){
	$(target).children("span").css("display","block");
}

function hidedelete(target){
	$(target).children("span").css("display","none");
}

//删除拍摄照片
function deletePic(){
	var imageId = $("#imageId").val();
	var infoId = $("#ID").val();
	$.post(ctx + "/propertyRepair/deleteImage", {
		imageId:imageId 
		},
	function(data) {
		data = JSON.parse(data);
		if (data.success) {
			layer.msg("操作成功", {
				icon : 1,
				time : 1000
			// 2秒关闭（如果不配置，默认是3秒）
			}, function() {
				window.parent.gotoShowDetails(infoId);
				parent.layer.close(index); // 根据窗口索引关闭窗口

			});
		} else {
			layer.msg(data.msg, {
				icon : 2,
				time : 1000
			// 2秒关闭（如果不配置，默认是3秒）
			}, function() {
				parent.layer.close(index); // 根据窗口索引关闭窗口
			});
		}
	});
}

//保存或新增方法
var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
function saveOrUpdate() {
	var title = $("#title").val();
	var houseId = $("#houseId").val();
	var contactor = $("#contactor").val();
	var contactorPhone = $("#contactorPhone") .val();
	var content = $("#content").val();
	var level = $("#level").val();
	var type = $("#type").val();
	
	var length = $("#imgLength").val();
	var imgArr = [];
	for(var i=1;i <= length;i++){
		if($("#imgId" + i).val() != null){
			imgArr.push($("#imgId" + i).val());
		}
	}
	
	if (checkTitle() && checkHouse() && checkContactor() && checkContactorPhone() && 
			checkContent() && checkLevel() /*&& checkType()*/) {
		var index1 = layer.load(1);
		$.ajax({
			type : "POST",
			url : ctx + "/propertyRepair/saveOrUpdate",
			cache : false,
			dataType : 'json',
			data: {
				title: title,
				houseId:houseId,
				contactor:contactor,
				contactorPhone:contactorPhone,
				content:content,
				level:level,
				type:type,
				imgArr : imgArr
			},
			success : function(result) {
				if (result.success) {
					layer.msg('提交成功', {
						icon: 1,
						time: 1000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
						layer.close(index1);
						parent.window.location.href= ctx + "/propertyRepair/infoList";
					});
				} else {
					layer.msg(result.msg, {
						icon: 2,
						time: 1000
					});
					layer.close(index1);
					parent.layer.close(index); // 根据窗口索引关闭窗口
				}
			}
		});
	}
}


function BackDetail() {
	parent.window.location.href= ctx + "/complaintsInfo/showDetails";
}


function shutComplainAndCommon(id) {
	var common = $("#common").val();
	var replys = {};
	replys.content = common;
	replys.repairId = id;
	
	if (checkCommon()) {
		var index1 = layer.load(1);
		$.post(ctx + "/propertyRepair/saveReply", replys, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					window.parent.gotoShowDetails(id);
					parent.layer.close(index); // 根据窗口索引关闭窗口

				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 1000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index1);
					parent.layer.close(index); // 根据窗口索引关闭窗口
				});
			}
		});
	}
}

function gotoShowDetails(id){
	$('#first').load(ctx+"/propertyRepair/showDetails?id="+id);
}

function BackList(){
	parent.window.location.href = ctx + "/propertyRepair/infoList";
}
